package com.qifenqian.turbot.widget

import android.app.Activity
import android.content.Context
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.qifenqian.turbot.R

/**
 * @author H
 * @createTime 2018/12/20 14:23
 * @description
 * @see
 * @since
 */
class TitleBarLayout : LinearLayout {

    constructor(context: Context) : super(context) {
        initAttrs(context, null, 0, 0)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initAttrs(context, attrs, 0, 0)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initAttrs(context, attrs, defStyleAttr, 0)
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        initAttrs(context, attrs, defStyleAttr, defStyleRes)
    }

    private var titleText: String? = null
    private var canBack: Boolean = true
    private var titlePadding: Int = 0
    private var titlePaddingLeft: Int = 0
    private var titlePaddingTop: Int = 0
    private var titlePaddingRight: Int = 0
    private var titlePaddingBottom: Int = 0

    private fun initAttrs(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_title_bar, this, false)
        addView(view)
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBarLayout, defStyleAttr, 0)
        titleText = typedArray.getString(R.styleable.TitleBarLayout_textTitle)
        canBack = typedArray.getBoolean(R.styleable.TitleBarLayout_canBack, true)
        titlePadding = typedArray.getDimension(R.styleable.TitleBarLayout_titlePadding, 0f).toInt()
        titlePaddingLeft = typedArray.getDimension(R.styleable.TitleBarLayout_titlePaddingLeft, 0f).toInt()
        titlePaddingTop = typedArray.getDimension(R.styleable.TitleBarLayout_titlePaddingTop, 0f).toInt()
        titlePaddingRight = typedArray.getDimension(R.styleable.TitleBarLayout_titlePaddingRight, 0f).toInt()
        titlePaddingBottom = typedArray.getDimension(R.styleable.TitleBarLayout_titlePaddingBottom, 0f).toInt()
        typedArray.recycle()
        val btnTitleBack = view.findViewById<ConstraintLayout>(R.id.btn_title_back)
        val tvTitleTitle = view.findViewById<TextView>(R.id.tv_title_title)
        if (canBack) {
            btnTitleBack.visibility = View.VISIBLE
        } else {
            btnTitleBack.visibility = View.GONE
        }
        btnTitleBack.setOnClickListener {
            //除非设置监听,不然默认是关闭界面
            if (onBackClickListener != null) {
                onBackClickListener!!.onClick()
            } else {
                (context as Activity).onBackPressed()
            }
        }
        if (titlePadding != 0) {
            view.setPadding(titlePadding, titlePadding, titlePadding, titlePadding)
        } else {
            view.setPadding(titlePaddingLeft, titlePaddingTop, titlePaddingRight, titlePaddingBottom)
        }
        tvTitleTitle.text = titleText
    }

    private var onBackClickListener: OnBackClickListener? = null
    fun setOnBackClickListener(listener: OnBackClickListener) {
        onBackClickListener = listener
    }

    interface OnBackClickListener {
        fun onClick()
    }
}
